﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MQC.record
{
    public partial class Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind(0);
                ClearClass(LiWait);
            }
        }
        public void Bind(int status)
        {
             using(DbAppDataContext db =new DbAppDataContext())
                {
                    var list = from m in db.Record
                               where m.Status == status && m.Del == 0
                               select new { Rid = m.Id, Batch = m.Batch,Machine=m.Machine, Maktx = m.Maktx, Customer = m.Customer,Status=m.Status};
                    var result = list.ToList();
                    GridView1.DataSource = result;
#if !DEBUG
                    GridView1.Columns[1].HeaderStyle.CssClass = "hidden";
                    GridView1.Columns[1].ItemStyle.CssClass = "hidden";
                    GridView1.Columns[2].HeaderStyle.CssClass = "hidden";
                    GridView1.Columns[2].ItemStyle.CssClass = "hidden";
                    GridView1.Columns[7].HeaderStyle.CssClass = "hidden";
                    GridView1.Columns[7].ItemStyle.CssClass = "hidden";
#endif
                    GridView1.Columns[9].HeaderStyle.CssClass = "hidden";
                    GridView1.Columns[9].ItemStyle.CssClass = "hidden";
                    BtnStart.Visible = false;
                    BtnReturn.Visible = false;
                    BtnEnd.Visible = false;
                    BtnWait.Visible = false;
                    BtnReStart.Visible = false;
                    //GridView1.Columns[6].Visible = false;
                    ShowObject(status);
                    GridView1.DataBind();
                }
        }
        public void BoardBind()
        {
            DateTime dt = DateTime.Now;
            using(DbAppDataContext db = new DbAppDataContext())
            {
                var list = from m in db.Record
                           orderby m.Id descending
                           where m.Del==0 && (m.Status==2 || m.Status==3) && m.Show==0 && Convert.ToDouble((dt - (DateTime)m.ChangeDate).TotalHours) <= 8
                           select new { Rid = m.Id, Batch = m.Batch, Machine = m.Machine, Maktx = m.Maktx, Customer = m.Customer, Status = m.Status };
                var result = list.ToList();
                GridView1.DataSource = result;
#if !DEBUG
                GridView1.Columns[1].HeaderStyle.CssClass = "hidden";
                GridView1.Columns[1].ItemStyle.CssClass = "hidden";
                GridView1.Columns[2].HeaderStyle.CssClass = "hidden";
                GridView1.Columns[2].ItemStyle.CssClass = "hidden";
               
#endif
                GridView1.Columns[7].HeaderStyle.CssClass = "hidden";
                GridView1.Columns[7].ItemStyle.CssClass = "hidden";
                GridView1.Columns[9].HeaderStyle.CssClass = "show";
                GridView1.Columns[9].ItemStyle.CssClass = "show";
                BtnStart.Visible = false;
                BtnReturn.Visible = false;
                BtnEnd.Visible = false;
                BtnWait.Visible = false;
                BtnReStart.Visible = false;
                BtnFa.Visible = false;
                BtnAddPerson.Visible = false;
                BtnAllowProduce.Visible = false;
                GridView1.DataBind();

            }
        }
        public void ShowObject(int status)
        {
            if (status == 0)
            {
                BtnStart.Visible = true;
            }
            else if (status == 1)
            {
                GridView1.Columns[6].Visible = true;
                BtnReturn.Visible = true;
                BtnEnd.Visible = true;
            }
            else if(status==2)
            {
                BtnReStart.Visible = true;
            }
            else if(status==3)
            {

            }
        }
        protected void WaitCheck_Click(object sender, EventArgs e)
        {  
            Bind(0);
            ClearClass(LiWait);
        }
        protected void Checking_Click(object sender, EventArgs e)
        {
            Bind(1);
            ClearClass(LiCheck);
        }
        protected void Back_Click(object sender, EventArgs e)
        {
            Bind(2);
            ClearClass(LiReturn);
        }
        protected void Complete_Click(object sender, EventArgs e)
        {
            Bind(3);
            ClearClass(LiEnd);
        }
        protected void Show_Click(object sender, EventArgs e)
        {
            BoardBind();
            ClearClass(LiShow);
        }
        public void ClearClass(HtmlGenericControl LiActive)
        {
            LiWait.Attributes.Remove("class");
            LiCheck.Attributes.Remove("class");
            LiReturn.Attributes.Remove("class");
            LiEnd.Attributes.Remove("class");
            LiShow.Attributes.Remove("class");
            LiActive.Attributes.Add("class", "active");

        }


        protected void BtnStart_Click(object sender, EventArgs e)
        {
            string bt =IsNoPersonLog(GridView1);
            string[] bts = bt.Split('_');
            if(bt.Length>0)
            {
                string s = string.Format("<script>alert('请先填写检验人员工号');</script>");
                Response.Write(s);
            }
            else
            {
                ChangeStatus(GridView1, 0, 1);
                ClearClass(LiWait);
            }
            
        }
           
        protected void BtnReStart_Click(object sender, EventArgs e)
        {
            ChangeStatus(GridView1, 2, 1);
            ClearClass(LiReturn);
        }

        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            ChangeStatus(GridView1, 1, 2);
            ClearClass(LiCheck);
        }

        protected void BtnEnd_Click(object sender, EventArgs e)
        {
            ChangeStatus(GridView1, 1, 3);
            ClearClass(LiCheck);
        }
        protected void BtnWait_Click(object sender, EventArgs e)
        {
            ChangeStatus(GridView1, 2, 0);
        }
        
        protected void BtnAddPerson_Click(object sender, EventArgs e)
        {
            bool cbx = false;
            bool choices = false;
            string  bt="";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbx = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                if (cbx)
                {
                    if (choices == false)
                    {
                        bt = ((Label)GridView1.Rows[i].FindControl("LbBatch")).Text;
                        choices = true;
                    }
                    else
                    {
                        bt = bt + "|" + ((Label)GridView1.Rows[i].FindControl("LbBatch")).Text;
                    }

                }
            }
            if (choices)
            {
                Response.Write("<script>var iTop = (window.screen.availHeight-130)/2;var iLeft = (window.screen.availWidth-410)/2;window.open('AddPersonNo.aspx?batch=" + bt + "&department=MQC','_blank','width=400,height=100,left='+iLeft+',top='+iTop+',menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes');</script>");
            }
            else
            {
                Response.Write("<script>alert('至少选择一行')</script>");
            }
        }

        protected void BtnFa_Click(object sender, EventArgs e)
        {
            using(DbAppDataContext db = new DbAppDataContext())
            {
                bool cbx = false;
                string bt = "";
                string[] strs = new string[4];
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    cbx = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                    if(cbx)
                    {
                        bt = ((Label)GridView1.Rows[i].FindControl("LbBatch")).Text;
                        strs = bt.Split('_');
                        Record data = db.Record.SingleOrDefault(c => c.Batch == bt);
                        FA_Record faData = new FA_Record();
                        if (data.FaStatues == -1)
                        {
                            data.FaStatues = 0;
                            data.FaCreateDate = DateTime.Now;
                            data.FaChangeDate = DateTime.Now;
                            faData.Batch = bt;
                            faData.Maktx = strs[1];
                            faData.Mold = strs[0];
                            faData.Proce = "MQC上模";
                            faData.CreateDate = DateTime.Now;
                            faData.ChangeDate = DateTime.Now;
                            faData.Status = 0;
                            faData.Num = 0;
                            db.FA_Record.InsertOnSubmit(faData);
                            db.SubmitChanges();
                            string s = string.Format("<script>alert('批次{0}添加成功');</script>", bt);
                            Response.Write(s);
                        }
                        else
                        {
                            string s = string.Format("<script>alert('批次{0}在FA检验中已存在');</script>", bt);
                            Response.Write(s);
                        }
                    }
                   
                }
            }
        }
        protected void BtnAllowProduce_Click(object sender, EventArgs e)
        {
            using (DbAppDataContext db = new DbAppDataContext())
            {
                bool cbx = false;
                string bt = "";
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    cbx = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                    if(cbx)
                    {
                        bt = ((Label)GridView1.Rows[i].FindControl("LbBatch")).Text;
                        Record data = db.Record.SingleOrDefault(c => c.Batch == bt);
                        data.AllowProduce = 1;
                        db.SubmitChanges();
                        string s = string.Format("<script>alert('批次{0}允许生产');</script>", bt);
                        Response.Write(s);
                    }
                   
                }
                
            }
        }
        /// <summary>
        /// 判断是否添加过至少一个检验员工号
        /// </summary>
        /// <param name="GridView1"></param>
        /// <returns></returns>
        public string IsNoPersonLog(GridView GridView1)
        {
            string result ="";
            bool cbx = false;
            string bt = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbx = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                if(cbx)
                {
                    bt = ((Label)GridView1.Rows[i].FindControl("LbBatch")).Text;
                }
                using (DbAppDataContext db = new DbAppDataContext())
                {
                    var list = from m in db.Person_Log
                               where m.Batch == bt
                               select m;
                    if(list.ToList().Count==0)
                    {
                        result = bt;
                        break;
                    }
                }
            }
            return result;
        }
        public void ChangeStatus(GridView GridView1,int nowStatus,int toStatus)
        {
            bool cbx = false;
            int  rid;
            for(int i=0;i<GridView1.Rows.Count;i++)
            {
                cbx = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                if(cbx)
                {
                    rid = int.Parse(((Label)GridView1.Rows[i].FindControl("Label1")).Text);
                    using(DbAppDataContext db =new DbAppDataContext())
                    {
                        Record data = db.Record.SingleOrDefault(c => c.Id == rid);
                        data.Status = toStatus;
                        data.ChangeDate = DateTime.Now;
                        //db.SubmitChanges();
                        Record_Log rl = new Record_Log();
                        rl.Batch = data.Batch;
                        rl.DateTime = DateTime.Now;
                        rl.FromStatus = nowStatus;
                        rl.ToStatus = toStatus;
                        rl.Department = "MQC";
                        db.Record_Log.InsertOnSubmit(rl);
                        db.SubmitChanges();                       
                    }
                }
            }
            Bind(nowStatus);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            if(e.CommandName=="Select")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                string batch = ((Label)GridView1.Rows[id].FindControl("LbBatch")).Text;
                Response.Redirect("Add.aspx?batch=" + batch + "");
            }
            if(e.CommandName=="PS2")
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
                int id = Convert.ToInt32(drv.RowIndex.ToString());
                string batch = ((Label)GridView1.Rows[id].FindControl("LbBatch")).Text;
                Response.Write("<script>window.open('PS2.aspx?batch=" + batch + "','备注','height=500px,width=800px,top=100px,left=200px');</script>");
            }
            if (e.CommandName == "DeleteBoard")
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
                int id = Convert.ToInt32(drv.RowIndex.ToString());
                string batch = ((Label)GridView1.Rows[id].FindControl("LbBatch")).Text;
                using(DbAppDataContext db = new DbAppDataContext())
                {
                    Record data = db.Record.SingleOrDefault(c => c.Batch == batch);
                    data.Show = 1;
                    db.SubmitChanges();
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Status = int.Parse(GridView1.Rows[0].Cells[7].Text.ToString());
            GridView1.PageIndex = e.NewPageIndex;
            Bind(Status);
        }

       

        

       

        




    }
}